package com.mew.common.dc.db;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mew.common.config.RedisConstantValue;
import com.mew.common.dc.mapper.JewelProductMapper;
import com.mew.common.dc.mapper.MoneyProductMapper;
import com.mew.common.entity.config.JewelProduct;
import com.mew.common.entity.config.MoneyProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class GoldBuyService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private JewelProductMapper jewelProductMapper;

    @Autowired
    private MoneyProductMapper moneyProductMapper;

    public void lodeJewelProduct(){
        try {
            redisTemplate.opsForHash().delete(RedisConstantValue.CONFIG_BUY_JEWEL);
        } catch (Exception e) {
        }

        List<JewelProduct> jewelProducts = jewelProductMapper.selectList(new LambdaQueryWrapper<JewelProduct>().eq(JewelProduct::getState, 1));
        for (JewelProduct jewelProduct : jewelProducts) {
            redisTemplate.opsForHash().put(RedisConstantValue.CONFIG_BUY_JEWEL,jewelProduct.getId(),jewelProduct);
        }
    }

    public List<JewelProduct> getJewelProductList(){
        List<Object> values = redisTemplate.opsForHash().values(RedisConstantValue.CONFIG_BUY_JEWEL);
        List<JewelProduct> list = values.stream().map(j -> (JewelProduct) j).collect(Collectors.toList());

        Collections.sort(list,(a,b)->a.getId().compareTo(b.getId()));

        return list;
    }

    public JewelProduct getJewelProduct(Integer id){
        Object o = redisTemplate.opsForHash().get(RedisConstantValue.CONFIG_BUY_JEWEL, id);
        return (JewelProduct)o;
    }


    public void lodeMoneyProduct(){
        try {
            redisTemplate.opsForHash().delete(RedisConstantValue.CONFIG_BUY_MONEY);
        } catch (Exception e) {
        }

        List<MoneyProduct> moneyProducts = moneyProductMapper.selectList(new LambdaQueryWrapper<MoneyProduct>().eq(MoneyProduct::getState, 1));
        for (MoneyProduct moneyProduct : moneyProducts) {
            redisTemplate.opsForHash().put(RedisConstantValue.CONFIG_BUY_MONEY,moneyProduct.getId(),moneyProduct);
        }
    }

    public List<MoneyProduct> getMoneyProductList(){
        List<Object> values = redisTemplate.opsForHash().values(RedisConstantValue.CONFIG_BUY_MONEY);
        List<MoneyProduct> list = values.stream().map(m -> (MoneyProduct) m).collect(Collectors.toList());

        Collections.sort(list,(a,b)->a.getMoney().compareTo(b.getMoney()));

        return list;
    }

    public MoneyProduct getMoneyProduct(String id){
        Object o = redisTemplate.opsForHash().get(RedisConstantValue.CONFIG_BUY_MONEY, id);
        return (MoneyProduct)o;
    }

}
